package com.leyou.page.service;

import com.leyou.page.client.BrandClient;
import com.leyou.page.client.CategoryClient;
import com.leyou.page.client.GoodsClient;
import com.leyou.page.client.SpecificationClient;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.thymeleaf.TemplateEngine;
import org.thymeleaf.context.Context;

import java.io.File;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.Map;

/**
 * @Author: wzh
 * @Date: 2019/4/10 19:42
 * @Description:生成静态页面，部署到nginx上，实现反向代理
 *
 * 部署时将ly-page微服务和nginx部署到一起
 */
@Slf4j
@Service
public class PageService {

    @Autowired
    private GoodsClient goodsClient;

    @Autowired
    private CategoryClient categoryClient;

    @Autowired
    private BrandClient brandClient;

    @Autowired
    private SpecificationClient specificationClient;


    @Autowired
    private TemplateEngine templateEngine;

    public Map<String, Object> loadModel(Long spuId) {
        Map<String,Object> model = new HashMap<>();
        //

        /*model.put("spu", spu);
        model.put("spu", spu);
        model.put("spu", spu);
        model.put("spu", spu);*/
        return null;
    }

    /**
     * 生成HTML文件
     * @param spuId
     */
    public void createHtml(Long spuId){
        //上下文
        Context context = new Context();
        context.setVariables(loadModel(spuId));

        //输出流
        File dest = new File("F:/leyoutool/nginx-1.12.2/html",spuId+".html");
        if (dest.exists()){
            dest.delete();
        }
        //生成HTML文件

    }

    /**
     * 删除HTML文件
     * @param spuId
     */
    public void deleteHtml(Long spuId) {
        File dest = new File("F:/leyoutool/nginx-1.12.2/html",spuId+".html");
        if (dest.exists()){
            dest.delete();
        }
    }
}
